import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Point;
import javax.swing.*;

import java.util.Vector;
@SuppressWarnings("serial")
public class Saolei extends JFrame{
	private LeiData leiData;
	private boolean gameOver;
	private int time=0;
	private int ge=9;
	//private int lei=10;
	SFram SLFram;
	ShowPanel showPanel;
	Timer toadd=new javax.swing.Timer(1000,new ActionListener(){
		public void actionPerformed(ActionEvent ae) {
			time++;
			showPanel.setTime(time);
			showPanel.setLei(SLFram.getShowLeave());
		}
	});
	Saolei(){
		this.addWindowListener(new WindowAdapter(){ 
            public void windowClosing(WindowEvent e){
                System.exit(0); 
            } 
		});
		this.setLayout(new BorderLayout(2,2));
		leiData=new LeiData();
		showPanel=new ShowPanel(this);
		SLFram=new SFram(this);
		this.add(showPanel,BorderLayout.NORTH);
		this.add(SLFram,BorderLayout.CENTER);
		//this.pack();
		this.setSize(350,400);
		this.setVisible(true);
		timerStart();
	}
	void reshow(){
		this.setVisible(true);
	}
	void setOpen(int x,int y){
		if(SLFram.setOpen(x,y,leiData.getType(x,y)))
			if(leiData.getType(x,y)==0){
				setBlank(x,y);
			}else if(leiData.getType(x,y)==9){
				gameStop();
			}
	}
	void setClose(int x,int y){
		if(leiData.getType(x,y)==9)
			SLFram.setClose(x,y,1);
		else
			SLFram.setClose(x,y,0);
	}
	void setBlank(int x,int y){
		Vector<Point> temLei=new Vector<Point>();
		temLei.addElement(new Point(x,y));	
		if(temLei.contains(new Point(x,y))){
			System.out.println("okokok");
		}
		//for(int index=0;index<temLei.size();index++)
		while(temLei.size()>0){
			Point tem1=temLei.firstElement();
			if(leiData.getType(tem1.x,tem1.y)==0){
				leiData.setType(tem1.x,tem1.y,10);
				for(int i=tem1.x-1;i<=tem1.x+1;i++){
					if(i<ge&&i>=0&&leiData.getType(i,tem1.y)<9){
						SLFram.setOpen(i,tem1.y,leiData.getType(i,tem1.y));
						if(leiData.getType(i,tem1.y)==0){
							if(!temLei.contains(new Point(i,tem1.y)))
							temLei.addElement(new Point(i,tem1.y));
						}
					}
				}
				for(int j=tem1.y-1;j<=tem1.y+1;j++){
					if(j<ge&&j>=0&&leiData.getType(tem1.x,j)<9){
						SLFram.setOpen(tem1.x,j,leiData.getType(tem1.x,j));
						if(leiData.getType(tem1.x,j)==0){
							if(!temLei.contains(new Point(tem1.x,j)))
							temLei.addElement(new Point(tem1.x,j));
						}
					}
				}
				temLei.removeElement(tem1);
			}
			else{
				temLei.removeElement(tem1);
			}
		}
		//if(leiData.getType(x+1, y)==0){
			//SLFram.setOpen(x+1,y,leiData.getType(x+1,y));
		//}
	}	
	public int getTime(){
		return time;
	}
	public void timerStart(){
		toadd.start();
	}
	public void timerStop(){
		toadd.stop();
	}
	public void gameStart(int lei,int ge){
		//this.lei=lei;
		this.ge=ge;
		time=0;
		gameOver=false;
		SLFram.replay(ge,lei);
		leiData.fillData(lei,ge);
		timerStart();
		if(ge==16)
			this.setSize(450,500);
		else if(ge==21)
			this.setSize(550,600);
		else if(ge==9)
			this.setSize(350,400);
		this.setVisible(true);
	}
	public void gameStop(){
		gameOver=true;
		timerStop();
		for(int i=0;i<ge;i++)
			for(int j=0;j<ge;j++){
				//System.out.println(i+"|"+j+"|"+ge);
				//if(leiData.getType(i,j)==9)
					SLFram.setOpen(i,j,leiData.getType(i,j));
			}
		JOptionPane.showMessageDialog(this,"Game Over");
	}
	public boolean isGameOver(){
		return gameOver;
	}
	public void win(){
		gameOver=true;
		timerStop();
		JOptionPane.showMessageDialog(this,"You Win The Game");
	}
	public static void main(String[] args){
		new Saolei();
	}
}
